package com.gaotong.science.controller;

import ch.qos.logback.core.BasicStatusManager;
import com.gaotong.science.service.HistoryService;
import com.gaotong.science.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

@RestController
public class Python {


      public static R runPython(String[] args) {
            Process proc;
            String message="";
            try {
                  proc = Runtime.getRuntime().exec(args);// 执行py文件
                  //用输入输出流来截取结果
                  BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
                  String line = null;

                  while ((line = in.readLine()) != null) {
                        System.out.println(line);
                        message=message+line;
                  }
                  if(message.contains("Success")){
                        String[] split = message.split(",");

                        return R.ok().data("url",split[1]);
                  }
                  in.close();
                  proc.waitFor();
            } catch (IOException e) {
                  e.printStackTrace();
            } catch (InterruptedException e) {
                  e.printStackTrace();
            }catch (Exception e){
                  e.printStackTrace();
            }
            return R.error().message(message);

      }
}